#include <stdlib.h>
#include <stdio.h>
#include "comic_packer.h"
#include "debug.h"
#include <etk/Etk.h>
#include <string.h>

Ecore_Evas * ee=NULL;
Evas 	   * evas=NULL;
Etk_Widget *embed;

int my_init() {
	if (!ecore_init() || !ecore_evas_init()) {
		error("Failed to init\n");
		return 0;
	}
	ee = ecore_evas_software_x11_new(NULL, 0,  0, 0, 600, 480);
	if (!ee) {
		error("Failed to initial ecore_evas\n");
		return 0 ;
	}
	evas = ecore_evas_get(ee);
	if (!evas) {
		error("Failed to initial evas\n");
		return 0 ;
	}
	if (!eet_init()) {
		error("Failed to initial eet\n");
		return 0 ;
	}
	ecore_evas_title_set(ee, "Thumbnail Viewer");
	ecore_evas_show(ee);
	evas_font_path_append(evas, "fonts/");
	return 1;
}


static void on_resize(Ecore_Evas *ee) {
	Evas *evas;
	int w,h;
	evas = ecore_evas_get(ee);
	evas_output_size_get(evas,&w,&h);
	evas_object_resize( etk_embed_object_get(ETK_EMBED(embed)),  w, h);
	evas_object_move(etk_embed_object_get(ETK_EMBED(embed)),0,0);
}

static int strcompare(const void *p1,const void *p2) {
	return strcmp(* (char * const *) p1, * (char * const *) p2);
}

int main (int args, char ** argv) {
	Eet_File *ef;
	char *filename;
	char **list;
	int num,i;
	Etk_Widget *tree;
	Etk_Tree_Col * col_key, *col_pic;
	if (args < 2) {
		printf("Usage %s <filename>\n",argv[0]);
		return EXIT_FAILURE;
	}
	filename = argv[1];

	if (!my_init()) {
		return EXIT_FAILURE;
	}
	etk_init(args,argv);
	ef = eet_open(filename,EET_FILE_MODE_READ);
	if (!ef) {
		error("open EET file %s failed !!\n",filename);
		return EXIT_FAILURE;
	}
	embed  = etk_embed_new(ecore_evas_get(ee));
	tree = etk_tree_new();
	etk_scrolled_view_policy_set(etk_tree_scrolled_view_get(ETK_TREE(tree)), 
			ETK_POLICY_AUTO, 
			ETK_POLICY_AUTO);
        etk_scrolled_view_dragable_set(
				 ETK_SCROLLED_VIEW(etk_tree_scrolled_view_get(ETK_TREE(tree))),
				 ETK_TRUE);
	etk_tree_mode_set(ETK_TREE(tree), ETK_TREE_MODE_LIST);
	etk_tree_multiple_select_set(ETK_TREE(tree), ETK_FALSE);
	col_key = etk_tree_col_new(ETK_TREE(tree), "Key" , 100, 0.0);
	col_pic = etk_tree_col_new(ETK_TREE(tree), "Image" , 500, 0.0);
	etk_tree_rows_height_set (ETK_TREE(tree), 640);
	etk_tree_col_model_add(col_key, etk_tree_model_text_new());
	etk_tree_col_model_add(col_pic, etk_tree_model_image_new());
	etk_tree_headers_visible_set(ETK_TREE(tree), 1);
	etk_tree_build(ETK_TREE(tree));
	etk_container_add(ETK_CONTAINER(embed), tree);
	evas_object_resize(etk_embed_object_get(ETK_EMBED(embed)),800,640);
	evas_object_move(etk_embed_object_get(ETK_EMBED(embed)),0 ,0);
	etk_widget_show_all(embed);

	list = eet_list(ef, "*", &num);
	

	debug("Numbers of images are %d\n",num);
	qsort(list,num,sizeof(char *),strcompare);
	for (i=0;i<num;i++) {
		etk_tree_row_append(ETK_TREE(tree),NULL,
				col_key,list[i],
				col_pic,filename,list[i],
				NULL);
	}
	etk_tree_thaw(ETK_TREE(tree));

	ecore_evas_callback_resize_set(ee, on_resize);
	etk_main();
	eet_close(ef);
	free(list);
	ecore_evas_free(ee);
	ecore_evas_shutdown();
	ecore_shutdown();	
	return EXIT_SUCCESS;
}
